package com.wxdc.nmgyt.nmgytwydc.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;
import com.wxdc.nmgyt.nmgytwydc.base.BasePersenter;
import com.wxdc.nmgyt.nmgytwydc.base.BaseView;
import com.wxdc.nmgyt.nmgytwydc.common.MyCircleProgressDialog;
import com.wxdc.nmgyt.nmgytwydc.dagger.component.DaggerFragmentComponent;
import com.wxdc.nmgyt.nmgytwydc.dagger.component.FragmentComponent;
import com.wxdc.nmgyt.nmgytwydc.dagger.module.FragmentModule;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.MainContract;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/4/11.
 */

public abstract class BaseFragment<T extends BasePersenter> extends Fragment implements BaseView{
    protected Activity mActivity;
    protected Context mContext;
    protected View mView;
    private  Unbinder mUnBinder;
    @Inject
    protected T mPersenter;
    protected CircleProgressDialog mCircleProgressDialog;

    public abstract int getmLayoutId();

    public abstract void initInject();
    protected FragmentComponent getFragmentComponent(){
        return DaggerFragmentComponent
                .builder()
                .appComponent(MyApp.getAppComponent())
                .fragmentModule(getFragmentModule())
                .build();
    }
    protected FragmentModule getFragmentModule(){
        return new FragmentModule(this);
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(getmLayoutId(), container, false);
        initInject();
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnBinder = ButterKnife.bind(this,view);
        if (mPersenter != null) {
            mPersenter.attachView(this);
        }
        initProgressDialog();
    }
    /*
        * 定义progressdialog
        * */
    private CircleProgressDialog initProgressDialog() {
        if (mCircleProgressDialog == null) {
            mCircleProgressDialog = new CircleProgressDialog(mContext);
            mCircleProgressDialog.setCancelable(false);
            mCircleProgressDialog.setText("加载中...");
            MyCircleProgressDialog.initCircleProgressDialog(mCircleProgressDialog);
        }
        return mCircleProgressDialog;
    }

    /*
    * 获取progressdialog
    * */
    protected CircleProgressDialog getCircleProgressDialog() {
        return mCircleProgressDialog;
    }
    /*
    * 显示progressdialog
    * */
    protected void showProgressDialog(){
        if (mCircleProgressDialog == null) {
            initProgressDialog();
        }
        mCircleProgressDialog.showDialog();
    }
    /*
   * 显示progressdialog
   * */
    protected void showProgressDialog(String msg){
        if (mCircleProgressDialog == null) {
            initProgressDialog();
        }
        mCircleProgressDialog.changeText(msg);
        mCircleProgressDialog.showDialog();
    }
    /*
    * 隐藏progressdialog
    * */
    protected  void hideProgressDialog(){
        if (mCircleProgressDialog != null && mCircleProgressDialog.isShowing()) {
            mCircleProgressDialog.dismiss();
        }
    }
    protected  boolean isProgressDialog(){
        return mCircleProgressDialog == null ? false : mCircleProgressDialog.isShowing();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnBinder.unbind();
        hideProgressDialog();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPersenter != null) {
            mPersenter.detachView();
        }
    }
}
